package com.hznu.ez4life.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hznu.ez4life.modules.entity.Item;
import com.hznu.ez4life.modules.entity.Needitem;
import com.hznu.ez4life.modules.dao.NeeditemDao;
import com.hznu.ez4life.modules.service.BuildingService;
import com.hznu.ez4life.modules.service.ItemService;
import com.hznu.ez4life.modules.service.ItemtypeService;
import com.hznu.ez4life.modules.service.NeeditemService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hznu.ez4life.myBeans.NeedItemSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@Service
public class NeeditemServiceImpl extends ServiceImpl<NeeditemDao, Needitem> implements NeeditemService {

    @Autowired
    BuildingService buildingService;
    @Autowired
    ItemService itemService;
    @Autowired
    ItemtypeService itemtypeService;

    @Override
    public List<Needitem> findListByUserId(Integer userId)
    {
        List<Needitem> needitems = this.selectList(new EntityWrapper<Needitem>().eq("user_id",userId));
        for(Needitem needitem : needitems)
        {
            setDetail(needitem);
        }
        return needitems;
    }

    @Override
    public List<Needitem> findBatchByCondition(NeedItemSearch needItemSearch)
    {
        return this.baseMapper.findBatchByCondition(needItemSearch);
    }

    @Override
    public Needitem findById(Integer id)
    {
        Needitem needitem = this.selectById(id);
        setDetail(needitem);
        return needitem;
    }

    private void setDetail(Needitem needitem)
    {
        if(needitem.getBuildingId()!=null)
        {
            needitem.setBuilding(buildingService.selectById(needitem.getBuildingId()));
        }
        if(needitem.getItemId()!=null)
        {
            needitem.setItem(itemService.findById(needitem.getItemId()));
        }
    }
}
